// Learn cc.Class:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html

var GameDefine = require("GameDefine");
var PlayerController = require("PlayerController");

cc.Class({
    extends: cc.Component,

    properties: {
        // foo: {
        //     // ATTRIBUTES:
        //     default: null,        // The default value will be used only when the component attaching
        //                           // to a node for the first time
        //     type: cc.SpriteFrame, // optional, default is typeof default
        //     serializable: true,   // optional, default is true
        // },
        // bar: {
        //     get () {
        //         return this._bar;
        //     },
        //     set (value) {
        //         this._bar = value;
        //     }
        // },
        btnDirection : {
            default : null,
            type : cc.Node,
        },

        btnDircetionCenter:{
            default : null, 
            type : cc.Node,
        },

        btnAcce:{
            default : null,
            type : cc.Button,
        },

        player : {
            default : null,
            type : cc.Node,
        },

        _btnDirectionPos : new cc.Vec2(0, 0),
        _btnDirectionW : 0.0,
        _btnCenterW : 0.0,
        _isTouchIn : false,
        _isAcce : false,

        _playerController : {
            default : null,
            type : PlayerController,
        }
    },

    start () {
        this._btnDirectionPos = this.btnDirection.position;
        this._btnDirectionW = this.btnDirection.width;
        this._btnCenterW = this.btnDircetionCenter.width * 2;

        this.node.on(cc.Node.EventType.TOUCH_START, this.touchStartEvent, this);
        this.node.on(cc.Node.EventType.TOUCH_MOVE, this.touchMoveEvent, this);
        this.node.on(cc.Node.EventType.TOUCH_END, this.touchEndEvent, this);
        this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.touchEndEvent, this);

        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_UP, this.onKeyUp, this);

        this._playerController = this.player.getComponent(PlayerController);

        this._isAcce = false;
    },

    touchStartEvent(event){
        this._isTouchIn = false;
        var scrennW = this.node.width;
        var scrennH = this.node.height;
        var touchPos = event.getLocation();
        if(touchPos.x < scrennW * 0.5 && touchPos.y < scrennH * 0.5){
            this._isTouchIn = true;
            this.btnDirection.x = touchPos.x - scrennW * 0.5;
            this.btnDirection.y = touchPos.y - scrennH * 0.5;
        }
    },

    touchMoveEvent(event){
        if(this._isTouchIn){
            var scrennW = this.node.width;
            var scrennH = this.node.height;
            var touchPos = event.getLocation();

            var btnPos = this.btnDirection.position;
            var btnPosXInScreen = btnPos.x + scrennW * 0.5;
            var btnPosYInScreen = btnPos.y + scrennH * 0.5;

            var distance = Math.sqrt(Math.pow(touchPos.x - btnPosXInScreen, 2) + Math.pow(touchPos.y - btnPosYInScreen, 2));
            var btnRadius = this._btnDirectionW - this._btnCenterW;
            if(distance <= btnRadius){
                this.btnDircetionCenter.x = touchPos.x - btnPosXInScreen;
                this.btnDircetionCenter.y = touchPos.y - btnPosYInScreen;
            }else{
                var offsetX = touchPos.x - btnPosXInScreen;
                var offsetY = touchPos.y - btnPosYInScreen;
                var ratio = offsetY / offsetX;
                var centerX = Math.sqrt(Math.pow(btnRadius, 2) / (Math.pow(ratio, 2) + 1));
                var centerY = Math.sqrt(Math.pow(btnRadius, 2) - Math.pow(centerX, 2));
                this.btnDircetionCenter.x = offsetX > 0?centerX:-centerX;
                this.btnDircetionCenter.y = offsetY > 0?centerY:-centerY;
            }
        }
    },

    touchEndEvent(event){
        this.btnDirection.position = this._btnDirectionPos;
        this.btnDircetionCenter.position = new cc.Vec2(0, 0);
    },

    acceEvent(event){
        if(event.type == cc.Node.EventType.TOUCH_START){
            this._isAcce = true;
        }else if(event.type == cc.Node.EventType.TOUCH_START || event.type == cc.Node.EventType.TOUCH_CANCEL){
            this._isAcce = false;
        }
    },

    onKeyDown(event){
        if(event.keyCode == cc.KEY.a){
            this._isAcce = true;
        };
        if(event.keyCode == cc.KEY.d){
        }
    },

    onKeyUp(event){
        if(event.keyCode == cc.KEY.a){
            this._isAcce = false;
        };
    },
    
    update (dt) {
        if(this._playerController != null){
            var distance = Math.sqrt(Math.pow(this.btnDircetionCenter.x, 2) + Math.pow(this.btnDircetionCenter.y, 2));
            if(distance >= this._btnDirectionW * 0.2){
                this._playerController.setOperateDirection(new cc.Vec2(this.btnDircetionCenter.x, this.btnDircetionCenter.y));
            }else{
                this._playerController.setOperateDirection(new cc.Vec2(0, 0));
            }

            if(this._isAcce){
                this._playerController.setOperateType(GameDefine.operateType.FAST);
            }else if(distance >= this._btnDirectionW * 0.2){
                this._playerController.setOperateType(GameDefine.operateType.NORMAL);
            }else{
                this._playerController.setOperateType(GameDefine.operateType.NULL);
            }
        }
    },
});
